define([
    'base',
    'msgTool',
    'bootstrapDialog',
    'bootstrapTable',
    'highcharts',
    'exporting',
    'highchartsCN',
    'jqViewer',
    'jqPrint',
    "css!global/plugins/viewer/viewer.min.css"
], function (base, msgTool, BootstrapDialog, highcharts, jqPrint) {
	//默认加载基础库
    base();


    return {
        list: function (currentAction) {
            var myTable = $('#dataList');
            var tableRefresh = function () {
                myTable.bootstrapTable('refresh', {url: currentAction + '/api.json'})
            };
            //table按需刷新动作
            var doAction = function (obj) {
                myTable.bootstrapTable('refresh', {url: currentAction + '/api.json?pid=' + obj.id})
            };

            //table当前行的响应事件
            window.operateEvents = {
                'click .accept': function (e, value, row) {
                          $.baseUtils.handleState({
                              row: row,
                              onText: '受理',
                              tableName: myTable,
                              nameText: '&nbsp;',
                              nameValue:row.applyName+'的'+row.serviceName,
                              //点击认领后，要将状态改为处理中，所以调用此接口。
                              url: '../handleState/handle'
                          });
                 },
                 'click .info': function (e, value, row) {
                     window.location.href = row.projId;
                 },
                'click .handle': function (e, value, row) {
                          window.location.href = "../update/" + row.projId;
                 },
                'click .status': function (e, value, row) {
                    $.baseUtils.oneStatus({
                        row: row,
                        tableName: myTable,
                        nameText: '流程',
                        nameValue: row.flowName,
                        url: '../status/action'
                    });
                },
                'click .delete': function (e, value, row) {
                    $.baseUtils.oneDelete({
                        row: row,
                        tableName: myTable,
                        nameText: '流程',
                        nameValue: row.flowName,
                        url: '../delete/action'
                    });
                }
            };

            //table表格
            myTable.bootstrapTable({
                url:currentAction+'/api.json', // 接口 URL 地址
                cache: false, // 不缓存
                striped: true, // 隔行加亮
                pagination: true, // 开启分页功能
                sidePagination: 'server',//服务端分页
                sortOrder: 'desc', // 设置排序为反序 desc
                fixedColumns: true,//自动修复
                queryParams: queryParams,//搜索附加参数
                smartDisplay: false, // 智能显示 pagination 和 cardview 等
                toolbar: ".toolBar", //顶部工具菜单
                toolbarAlign: "right", //顶部工具位置
                mobileResponsive: "true", //手机模式自适应宽度（自动变成卡片模式）
                columns: [{
                    field: 'promiseTime',
                    title: '预警',
                    align: 'center',
                    width: '5%',
                    formatter: function (value, row, index) {
                    	var curdate=new Date();
                    	var flag=1;
                    	if(curdate>value){
                    		flag=2;
                    	};
                        var result = "<i class=\"fa fa-2x fa-smile-o text-primary\" aria-hidden=\"true\"></i>";
                        switch (flag) {
                            case 1:
                                result = "<i class=\"fa fa-2x fa-smile-o text-primary\" aria-hidden=\"true\"></i>";
                                break;
                            case 2:
                                result = "<i class=\"fa fa-2x fa-meh-o text-warning\" aria-hidden=\"true\"></i>";
                                break;
                            case 3:
                                result = "<i class=\"fa fa-2x fa-frown-o text-danger\" aria-hidden=\"true\"></i>";
                                break;
                        }
                        return result;
                    }
                },
                    {
                        field: 'projId',
                        title: '办件编号'
                    }, {
                        field: 'serviceName',
                        title: '事项信息',
                        formatter: function (value, row, index) {
                            return '<i class="fa fa-hashtag text-primary" aria-hidden="true"></i>&emsp;<b>' + value + '</b><br /><i class="fa fa-hashtag text-primary" aria-hidden="true"></i>&emsp;<span class="text text-muted">'+row.serviceCode+'</span><i class="fa fa-hashtag text-info" aria-hidden="true"></i>&emsp;<span class="text text-info">'+ row.deptName +'</span>'
                        }
                    },
                    {
                        field: 'applyName',
                        title: '申报人信息',
                        formatter: function (value, row, index) {
                            return '<i class="fa fa-user text-primary" aria-hidden="true"></i>&emsp;<b>' + value + '</b><br /><i class="fa fa-clock-o text-primary" aria-hidden="true"></i>&emsp;<span class="text text-muted">' + row.receiveTime + '</span>'
                        }
                    },
                   {
                        field: 'updateName',
                        title: '更新信息',
                        formatter: function (value, row, index) {
                            return '<i class="fa fa-user text-primary" aria-hidden="true"></i>&emsp;<b>' + value + '</b><br /><i class="fa fa-clock-o text-primary" aria-hidden="true"></i>&emsp;<span class="text text-muted">' + row.updateTime + '</span>'
                        }
                    }, {
                        field: 'handleState',
                        title: '办件状态',
                        align: 'center',
                        formatter: function (value, row, index) {
                                return '<span class="label label-primary">'+value+'</span>';
                        }
                     }, {
                        field: 'action',
                        title: '操作',
                        align: 'center',
                        formatter: operateFormatter,
                        events: operateEvents
                    }
                ]
            });

            //搜索刷新
            $('#search').on('click', function () {
                tableRefresh();
            });

            //toolbar按钮相应事件
            //新增
            $(".toolbar-save").on('click', function () {
                winModal("新增", null);
            });
            //刷新
            $(".toolbar-refresh").on('click', function () {
                tableRefresh();
            });

            //批量修改状态
            $(".toolbar-batchStatus").on('click', function () {
                var entityList = myTable.bootstrapTable('getSelections');
                var row = [], names = [];
                $.each(entityList, function (i, v) {
                    names.push(v.flowName);
                    row.push(v.id);
                });
                if (row.length > 0) {
                    $.baseUtils.batchAction({
                        row: row,
                        tableName: myTable,
                        nameText: '流程',
                        nameValue: names,
                        url: '../status/batchAction'
                    });
                } else {
                    msgTool.error("请选择虽少一条记录后再操作", "信息提示");
                }

            });


            //搜索参数传递
            function queryParams(params) {
               params.search = $(".searchValue").val().trim();
                return {
                    limit: params.limit,
                    offset: params.offset,
                    search: params.search
                };
            }

            //回显
            function responseHandler(res) {
                return res.rows;
            }


            //模态框
            function winModal(title, row) {
                BootstrapDialog.show({
                    type: BootstrapDialog.TYPE_DEFAULT,
                    tabindex: null,//解决select2搜索框失去焦点的问题
                    title: title,
                    closable: true,	//关闭箭头启动
                    closeByBackdrop: false,	//点击空白不消失
                    closeByKeyboard: true,//ESC取消禁用
                    message: $('<div></div>').load('../save/form.html'),
                    data: {
                        'row': row
                    },
                    buttons: [{
                        label: '取消',
                        cssClass: 'btn-white cancel',
                        icon: 'fa fa-times',
                        action: function (dialog) {
                            dialog.close();
                        }
                    }, {
                        label: '提交' + title,
                        //hotkey: 13,//回车提交
                        cssClass: 'btn-primary',
                        icon: 'fa fa-check',
                        action: function (dialog) {
                            //返回验证结果
                            var flag = false;
                            flag = $("#winModal").validate().form();
                            if (flag) {
                                $.baseUtils.ajax({
                                    dialog: dialog,
                                    url: '../save/action',
                                    data: $("#winModal").serialize(),
                                    text: "保存",
                                    isTable: true,
                                    tableName: '#flowList',
                                    isDialog: true
                                })
                            }
                        }
                    }],
                    onshown: function (dialog) {
                        if (row != null) {
                            $('#id').val(row.id);
                            $('#flowName').val(row.flowName);
                            $('#itemCode').val(row.itemCode);
                        }
                    }
                });
            }
        },
        view: function () {
            $("#doSubmit").on('click', function () {
                var data = {
                	projId: $("#projId").val(),
                	handleState: $("input[name='handleState']:checked").val(),
                	opinion: $("#opinion").val()
                };
                $.ajax({
                    url: "/examine/update/flow",
                    type: 'POST',
                    data: data,
                    dataType: "JSON",
                    beforeSend: function () {
                        //触发ajax请求开始时执行
                        $("#doSubmit").text('提交中...');
                        $("#doSubmit").attr('disabled', 'disabled');
                    },
                    success: function (data) {
                        if (data.success) {
                            window.location.href = "/examine/view/index";
                        } else {
                            msgTool.error(data.msg);
                        }
                    },
                    error: function () {
                        msgTool.error("操作失败，请刷新当前页面后重试！");
                    },
                    complete: function () {
                        $("#doSubmit").text("提交审核");
                        $("#doSubmit").removeAttr('disabled');
                    }
                });
            });
        }
    };
});